﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 
    /// </summary>
    public partial class Daily
    {

        /// <summary>
        /// 得到第几周
        /// </summary>
        /// <param name="i">参数</param>
        /// <returns>数据表</returns>
        public DataSet GetDay(int i)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select convert(char(10),dateadd");
            strSql.Append("(day," + (i + 1) + "-(datepart(weekday,dateadd(week,-2,getdate()))+");
            strSql.Append("@@datefirst-2)%7-1,dateadd(week,-2,getdate())),102) as wk ");

            return DbHelperSQL.Query(strSql.ToString());
            
        }


        /// <summary>
        /// 得到这天是不是假日
        /// </summary>        
        /// <param name="date">时间</param>
        /// <returns>数据表</returns>
        public DataSet GetHol(DateTime date) {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select FWorkDate ,count(*) as cou from t_WorkCalendar ");
            strSql.Append(" where Fdeftype <> '1' and FWorkDate=@FWorkDate");
            strSql.Append(" group by FWorkDate");

            SqlParameter[] parameters = {
                    new SqlParameter("@FWorkDate", SqlDbType.DateTime)
			};
            parameters[0].Value = date;
            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 不计算休息
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public string Pd(DateTime date)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("SELECT Fdeftype FROM dbo.t_WorkCalendar ");
            strSql.Append(" WHERE FWorkDate=@FWorkDate");
            SqlParameter[] parameters = {
                    new SqlParameter("@FWorkDate", SqlDbType.DateTime)
			};
            parameters[0].Value = date;

            return DbHelperSQL.GetString(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="entityNo">公司编号</param>
        /// <param name="emplNo">员工编号</param>
        /// <param name="date">时间</param>
        /// <returns>是否存在该记录</returns>
        public bool Exists(string entityNo, string emplNo, DateTime date)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from dbo.t_Daily");
            strSql.Append(" where  FEntityNo=@FEntityNo and FEmplNo=@FEmplNo  and FDate=@FDate");
            SqlParameter[] parameters = {
                    new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),					
					new SqlParameter("@FDate", SqlDbType.DateTime)
			};
            parameters[0].Value = entityNo;
            parameters[1].Value = emplNo;
            parameters[2].Value = date;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到这周的所有天数
        /// </summary>
        /// <param name="i">参数</param>
        /// <param name="gowk">参数</param>
        /// <returns>数据表</returns>
        public DataSet GetAllDay(int i, int gowk)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select convert(char(10),dateadd(day," + (i + 1) + "-");
            strSql.Append("(datepart(weekday,dateadd(week," + gowk + ",getdate()))+");
            strSql.Append("@@datefirst-2)%7-1,dateadd(week," + gowk + ",getdate())),102) as wk");

            return DbHelperSQL.Query(strSql.ToString());

        }




        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>数据添加</returns>
        public decimal Add(ElingOA6_Admin_DSJCJ.Model.Daily model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into dbo.t_Daily(");
            strSql.Append("FEmplNo,FEntityNo,FDate,FContent,FInsDate,FUpdDate,FInsIP)");
            strSql.Append(" values (");
            strSql.Append("@FEmplNo,@FEntityNo,@FDate,@FContent,@FInsDate,@FUpdDate,@FInsIP)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FDate", SqlDbType.DateTime),
					new SqlParameter("@FContent", SqlDbType.NVarChar,-1),
					new SqlParameter("@FInsDate", SqlDbType.DateTime),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FInsIP", SqlDbType.NVarChar,15)};
            parameters[0].Value = model.FEmplNo;
            parameters[1].Value = model.FEntityNo;
            parameters[2].Value = model.FDate;
            if (model.FContent == "")
            {
                parameters[3].Value = DBNull.Value;
            }
            else {
                parameters[3].Value = model.FContent;
            }
            parameters[4].Value = model.FInsDate;
            parameters[5].Value = model.FUpdDate;
            parameters[6].Value = model.FInsIP;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">model对象</param>
        /// <returns>是否更新</returns>
        public bool Update(ElingOA6_Admin_DSJCJ.Model.Daily model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update dbo.t_Daily set ");
            strSql.Append("FContent=@FContent,");
            strSql.Append("FUpdDate=@FUpdDate");
            strSql.Append(" where FDailyID=@FDailyID");
            SqlParameter[] parameters = {
					new SqlParameter("@FContent", SqlDbType.NVarChar,-1),
					new SqlParameter("@FUpdDate", SqlDbType.DateTime),
					new SqlParameter("@FDailyID", SqlDbType.Decimal,13)};
            if (model.FContent == "")
            {
                parameters[0].Value = DBNull.Value;
            }
            else
            {
                parameters[0].Value = model.FContent;
            }
            parameters[1].Value = model.FUpdDate;
            parameters[2].Value = model.FDailyID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="fDate">时间</param>
        /// <param name="fEmplNo">员工编号</param>
        /// <param name="fEntityNo">公司编号</param>
        /// <returns>是否存在</returns>
        public bool Exists(DateTime fDate, string fEmplNo, string fEntityNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_Daily");
            strSql.Append(" where FDate=@FDate and FEmplNo=@FEmplNo and FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FDate", SqlDbType.DateTime),
                    new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
			};
            parameters[0].Value = fDate;
            parameters[1].Value = fEmplNo;
            parameters[2].Value = fEntityNo;
           

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="fDate">时间</param>
        /// <param name="fEmplNo">员工编号</param>
        /// <param name="fEntityNo">公司编号</param>
        /// <returns>得到一个对象</returns>
        public ElingOA6_Admin_DSJCJ.Model.Daily GetModel(DateTime fDate, string fEmplNo, string fEntityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FDailyID,FEmplNo,FEntityNo,FDate,FContent,FInsDate,FUpdDate,FInsIP from dbo.t_Daily ");
            strSql.Append(" where FDate=@FDate and FEmplNo=@FEmplNo and FEntityNo=@FEntityNo");
            SqlParameter[] parameters = {
					new SqlParameter("@FDate", SqlDbType.DateTime),
                    new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)
			};
            parameters[0].Value = fDate;
            parameters[1].Value = fEmplNo;
            parameters[2].Value = fEntityNo;

            ElingOA6_Admin_DSJCJ.Model.Daily model = new ElingOA6_Admin_DSJCJ.Model.Daily();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="row">行</param>
        /// <returns>Model</returns>
        public ElingOA6_Admin_DSJCJ.Model.Daily DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.Daily model = new ElingOA6_Admin_DSJCJ.Model.Daily();
            if (row != null)
            {
                if (row["FDailyID"] != null && row["FDailyID"].ToString() != "")
                {
                    model.FDailyID = decimal.Parse(row["FDailyID"].ToString());
                }
                if (row["FEmplNo"] != null)
                {
                    model.FEmplNo = row["FEmplNo"].ToString();
                }
                if (row["FEntityNo"] != null)
                {
                    model.FEntityNo = row["FEntityNo"].ToString();
                }
                if (row["FDate"] != null && row["FDate"].ToString() != "")
                {
                    model.FDate = DateTime.Parse(row["FDate"].ToString());
                }
                if (row["FContent"] != null)
                {
                    model.FContent = row["FContent"].ToString();
                }
                if (row["FInsDate"] != null && row["FInsDate"].ToString() != "")
                {
                    model.FInsDate = DateTime.Parse(row["FInsDate"].ToString());
                }
                if (row["FUpdDate"] != null && row["FUpdDate"].ToString() != "")
                {
                    model.FUpdDate = DateTime.Parse(row["FUpdDate"].ToString());
                }
                if (row["FInsIP"] != null)
                {
                    model.FInsIP = row["FInsIP"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 得到日志内容
        /// </summary>
        /// <param name="beginDate">日期</param>
        /// <param name="empNo">员工编号</param>
        /// <returns>数据表</returns>
        public DataSet GetDailyText(string beginDate, string empNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from dbo.t_Daily");
            strSql.Append(" where FDate=@FDate and FEmplNo=@FEmplNo");

            SqlParameter[] parameters = {
                    new SqlParameter("@FDate", SqlDbType.DateTime),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15)
			};
            parameters[0].Value = beginDate;
            parameters[1].Value = empNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);

        }

        /// <summary>
        /// 得到当天人员的去留情况
        /// </summary>
        /// <param name="beginDate">日期</param>
        /// <param name="empNo">员工编号</param>
        /// <returns>数据表</returns>
        public DataSet GetLeave(string beginDate, string empNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from dbo.t_Leave");
            strSql.Append(" where DATEDIFF(DAY,FBeginDate,@FBeginDate)<=0");
            strSql.Append(" AND DATEDIFF(DAY,FEndDate,@FEndDate)>=0");
            strSql.Append(" AND FEmplNo=@FEmplNo");

            SqlParameter[] parameters = {
                    new SqlParameter("@FBeginDate", SqlDbType.DateTime),
                    new SqlParameter("@FEndDate", SqlDbType.DateTime),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15)
			};
            parameters[0].Value = beginDate;
            parameters[1].Value = beginDate;
            parameters[2].Value = empNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到人员离开的原因
        /// </summary>
        /// <param name="beginDate">日期</param>
        /// <param name="empNo">人员编号</param>
        /// <returns>数据列表</returns>
        public DataSet GetLeaveReason(string beginDate, string empNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *,(select FLeaveReasonName from dbo.t_cdLeaveReason ");
            strSql.Append(" where FLeaveReasonCd =a.FLeaveReasonCd)  as FLeaveReasonName ");
            strSql.Append(" from dbo.t_Leave as a");
            strSql.Append(" where DATEDIFF(DAY,FBeginDate,@FBeginDate)<=0");
            strSql.Append(" AND DATEDIFF(DAY,FEndDate,@FEndDate)>=0");
            strSql.Append(" AND FEmplNo=@FEmplNo");

            SqlParameter[] parameters = {
                    new SqlParameter("@FBeginDate", SqlDbType.DateTime),
                    new SqlParameter("@FEndDate", SqlDbType.DateTime),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15)
			};
            parameters[0].Value = beginDate;
            parameters[1].Value = beginDate;
            parameters[2].Value = empNo;

            return DbHelperSQL.Query(strSql.ToString(), parameters);
        
        }


    }
}
